<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" id="username" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">手机</label>
                    <div class="layui-input-block">
                        <input type="text" name="mobile" id="mobile" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                {{--        <div class="layui-inline">--}}
                {{--            <label class="layui-form-label">邮箱</label>--}}
                {{--            <div class="layui-input-block">--}}
                {{--                <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">--}}
                {{--            </div>--}}
                {{--        </div>--}}
                {{--        <div class="layui-inline">--}}
                {{--            <label class="layui-form-label">角色</label>--}}
                {{--            <div class="layui-input-block">--}}
                {{--                <select name="role">--}}
                {{--                    <option value="0">管理员</option>--}}
                {{--                    <option value="1">超级管理员</option>--}}
                {{--                    <option value="2">纠错员</option>--}}
                {{--                    <option value="3">采购员</option>--}}
                {{--                    <option value="4">推销员</option>--}}
                {{--                    <option value="5">运营人员</option>--}}
                {{--                    <option value="6">编辑</option>--}}
                {{--                </select>--}}
                {{--            </div>--}}
                {{--        </div>--}}
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary" lay-submit
                            lay-filter="LAY-user-back-search-reset">重置
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
            </div>
        </div>
    </div>
    <table class="layui-hide" id="LAY-table" lay-filter="LAY-table"></table>

</div>


<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<script src="../../layuiadmin/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        table.render({
            elem: '#LAY-table'
            , url: '{{url('manager/user/search')}}'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter1', 'exports1', 'print1', {
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据'
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                //  ,groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页

            }
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'username', title: '用户名', width: 120, edit: 'text'}
                , {field: 'nick_name', title: '昵称', width: 120}
                , {field: 'mobile', title: '手机', width: 120}
                , {
                    field: 'email', title: '邮箱', width: 150, edit: 'text', templet: function (data) {
                        return '<em>' + data.email + '</em>'
                    }
                }
                , {
                    field: 'status', title: '状态', width: 80, templet: function (data) {
                        var status = '<button class="layui-btn layui-btn-xs">正常</button>';
                        if (data.status ===1) {
                            status= '<button class="layui-btn layui-btn-xs layui-btn-danger">禁止</button>';
                        }
                        return status;
                    }
                }
                , {field: 'last_ip', title: 'IP', width: 120}
                , {field: 'created_at', title: '最后登录', width: 120, sort: true}
                , {
                    field: 'created_at', title: '加入时间', width: 120, templet: function (data) {
                        var date1 = new Date(data.created_at).toJSON();
                        var date = new Date(+new Date(date1) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
                        if (date === '1970-01-01 08:00:00') {
                            date = ''
                        }
                        date = date.substr(0, 10); //接取2020-04-02
                        return date;
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}

            ]]
            //  , page: true
            //  ,limit:1 //每页显示条数

        });
        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-table', {
                where: field
                // query: {
                //     username: demoReload.val()
                // }
            });
        });
        //监听搜索重置
        form.on('submit(LAY-user-back-search-reset)', function (data) {
            var field = data.field;
            $('#username').val('');
            $('#mobile').val('');
            //   console.log(data, $('#search_form'));
        });
        //头工具栏事件
        table.on('toolbar(LAY-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(LAY-table)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-table')
                    , checkData = checkStatus.data; //得到选中的数据
                var arr = [];
                $.each(checkData, function (index, item) {
                    //获取要删除的ID
                    arr.push(item.id);
                });
                //   console.log(arr);

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除吗？', function () {
                    console.log(arr);

                    //执行 Ajax 后重载
                    /*
                    admin.req({
                      url: 'xxx'
                      //,……
                    });
                    */
                    table.reload('LAY-table');
                    layer.msg('已删除');
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , title: '添加管理员'
                    , content: '/manager/user/form'
                    , area: ['500px', '500px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
